<template>
  <a-modal
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :title="title"
      :visible="visible"
      :width="520"
      class="noborder"
      @cancel="handleCancel"
      @ok="handleSubmit"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" class="mb-5" label="会员ID">
          <span>{{ record.id }}</span>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" class="mb-5" label="当前积分">
          <span class="c-p">{{ record.points | fixed }}</span>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="调整方式">
          <a-radio-group
              v-decorator="[`type`, { initialValue: RuleTypeEnum.ACQUIRE.key, rules: [{ required: true }] }]"
          >
            <a-radio
                v-for="(item, index) in RuleTypeEnum.data" :key="index" :value="item.key">
              {{ item.name }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="变更数量">
          <a-input-number
              v-decorator="[`points`, { initialValue: '', rules: [{ required: true, message: '请输入变更的金数量' }] }]"
              :min="0.01"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="管理员备注">
          <a-textarea
              v-decorator="[`reason`, { rules: [{ required: true, message: '请输入管理员备注' }] }]"
              :rows="3"
              placeholder="请输入管理员备注"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
// import pick from 'lodash.pick'
import * as Api from '@/api/market/points'
import RuleTypeEnum from "@/common/enum/points/RuleType";

export default {
  components: {},
  data() {
    return {
      RuleTypeEnum,
      // 对话框标题
      title: '会员充值',
      // 标签布局属性
      labelCol: {span: 7},
      // 输入框布局属性
      wrapperCol: {span: 13},
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {}
    }
  },
  methods: {

    // 显示对话框
    handle(record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {form: {validateFields}} = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    // 关闭对话框事件
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    // 提交到后端api
    onFormSubmit(values) {
      const {record} = this
      this.confirmLoading = true
      Api.adjust({userId: record.id, ...values})
          .then(result => {
            // 显示成功
            this.$message.success('操作成功', 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally(() => this.confirmLoading = false)
    }

  }
}
</script>
<style lang="less" scoped>
.ant-modal-body {
  padding-top: 0 !important;
}

.ant-form-item {
  margin-bottom: 15px;
}

.ant-tabs-nav .ant-tabs-tab {
  padding: 10px 16px;
}
</style>
